<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="名称" prop="course_name">
        <el-input v-model="dataForm.course_name" :maxlength="30"></el-input>
      </el-form-item>

      <el-form-item label="排序" prop="order_index">
        <el-input-number placeholder="请输入" class="input" maxlength="8" :min="0" :controls="false" :max="10000" :precision="0" v-model="dataForm.order_index">
        </el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import serviceApi from '@/api/serviceApi.js'
import Imglist from './../../oss/oss-uploadImg'
export default {
  components: {
    Imglist
  },
  data () {
    return {
      visible: false,
      is_pass: 1,
      dataForm: {
        course_name: null,
        order_index: 0
      },

      dataRule: {
        course_name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        order_index: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    init (row) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm = {
          course_name: null,
          order_index: 0
        }

        if (row) {
          this.dataForm = JSON.parse(JSON.stringify(row))
        }
      })
    },
    dataFormSubmit () {
      this.$refs['dataForm'].validate(async (valid) => {
        if (valid) {
          let res
          if (this.dataForm.id) {
            res = await serviceApi.classifyUpdate(this.dataForm)
            if (res && res.data.code == 200) {
              this.$message.success('操作成功')
              this.visible = false
              this.$emit('getList')
            }
          } else {
            res = await serviceApi.classifyAdd(this.dataForm)
            if (res && res.data.code == 200) {
              this.$emit('getList')
              this.$message.success('操作成功')
              this.visible = false
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
